package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.shop.dao.ShopOrderCouponDao;
import ysq.xyg.shop.model.po.ShopOrderCoupon;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.shop.model.vo.ShopOrderCouponVo;
import ysq.xyg.shop.service.ShopOrderCouponService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 商城模块-商品劵管理service实现
 *
 * @author congcong tao
 * @date 2019-4-28 11:19:33
 */
@Service("shopOrderCouponService")
public class ShopOrderCouponServiceImpl extends BaseServiceImpl<ShopOrderCoupon> implements ShopOrderCouponService {

    @Resource
    private ShopOrderCouponDao shopOrderCouponDao;

    private ShopOrderCoupon shopOrderCoupon;

    @Autowired
    public void setBaseDao(ShopOrderCouponDao shopOrderCouponDao) {
        this.baseDao = shopOrderCouponDao;
    }


    /**
     * 分页查询
     *
     * @param shopOrderCouponVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:19:33
     */
    public ReturnEntity page(ShopOrderCouponVo shopOrderCouponVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopOrderCoupon> list = acceptList(shopOrderCouponVo);
        PageInfo<ShopOrderCoupon> page=new PageInfo<ShopOrderCoupon>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageLeft(ShopOrderCouponVo shopOrderCouponVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<ShopOrderCoupon> list = shopOrderCouponDao.selectEntityList(shopOrderCouponVo);
        PageInfo<ShopOrderCoupon> page=new PageInfo<ShopOrderCoupon>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopOrderCouponVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:19:33
     */
    public ReturnEntity addOrUpdate(ShopOrderCouponVo shopOrderCouponVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopOrderCouponVo.getId())){
            shopOrderCoupon=new ShopOrderCoupon(currentUser);
            isUpate=false;
        }else{
            shopOrderCoupon=selectByPrimaryKey(shopOrderCouponVo.getId());
            if(ObjectUtil.isNull(shopOrderCoupon)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopOrderCoupon.setUpdateTime(new Date());
            shopOrderCoupon.setUpdateUser(currentUser.getId());
        }
        shopOrderCoupon.setMemberId(shopOrderCouponVo.getMemberId());
        shopOrderCoupon.setItemId(shopOrderCouponVo.getItemId());
        shopOrderCoupon.setItemName(shopOrderCouponVo.getItemName());
        shopOrderCoupon.setBrandName(shopOrderCouponVo.getBrandName());
        shopOrderCoupon.setCatName(shopOrderCouponVo.getCatName());
        shopOrderCoupon.setImage(shopOrderCouponVo.getImage());
        shopOrderCoupon.setQrImage(shopOrderCouponVo.getQrImage());
        shopOrderCoupon.setPrice(shopOrderCouponVo.getPrice());
        shopOrderCoupon.setStatus(shopOrderCouponVo.getStatus());
        shopOrderCoupon.setIssueStatus(shopOrderCouponVo.getIssueStatus());
        shopOrderCoupon.setIssueMessage(shopOrderCouponVo.getIssueMessage());
        shopOrderCoupon.setUploadTime(shopOrderCouponVo.getUploadTime());
        shopOrderCoupon.setManageTime(shopOrderCouponVo.getManageTime());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopOrderCoupon);
        }else{
            num=insert(shopOrderCoupon);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:19:33
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopOrderCoupon shopOrderCoupon=selectByPrimaryKey(id);
        if(shopOrderCoupon!=null){
            shopOrderCoupon.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopOrderCoupon.setUpdateTime(new Date());
            shopOrderCoupon.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopOrderCoupon);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopOrderCouponVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-4-28 11:19:33
     */
    public List<ShopOrderCoupon> acceptList(ShopOrderCouponVo shopOrderCouponVo){
        Example example = new Example(ShopOrderCoupon.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopOrderCouponVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",shopOrderCouponVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return shopOrderCouponDao.selectByExample(example);
    }
}
